// DBOptions.h: interface for the CDBOptions class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_DBOPTIONS_H__E93FD8E0_9334_4962_8768_BDBA90FAA470__INCLUDED_)
#define AFX_DBOPTIONS_H__E93FD8E0_9334_4962_8768_BDBA90FAA470__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#pragma comment (lib, "Configure")

template<class CStorage>
class CDBOptions  
{
// Functions
public:
	CDBOptions()
		: _isLoaded(FALSE)
		, _pszSqlServer(NULL)
		, _pszDatabaseName(NULL)
		, _dwAuthMode(0)
		, _pszUserName(NULL)
		, _pszPassword(NULL)
	{
	}
	virtual ~CDBOptions()
	{
		if (NULL != _pszSqlServer)
		{
			memset(_pszSqlServer, 0, strlen(_pszSqlServer));
			delete [] _pszSqlServer;
		}
		if (NULL != _pszDatabaseName)
		{
			memset(_pszDatabaseName, 0, strlen(_pszDatabaseName));
			delete [] _pszDatabaseName;
		}
		if (NULL != _pszUserName)
		{
			memset(_pszUserName, 0, strlen(_pszUserName));
			delete [] _pszUserName;
		}
		if (NULL != _pszPassword)
		{
			memset(_pszPassword, 0, strlen(_pszPassword));
			delete [] _pszPassword;
		}
	}
	BOOL LoadOptions()
	{
		if (TRUE == _isLoaded)
			return FALSE;	// Already loaded
		else
			return TRUE;
	}
	CString GetConnectString()
	{
		if (FALSE == _isLoaded)
		{
			if (FALSE == LoadOptions())
				return "";
		}

		_strConnect= CString()
					+ "Provider=SQLOLEDB.1;"
					+ "Data Source='"+ _pszSqlServer +"';"
					+ "Initial Catalog='"+ _pszDatabaseName +"';";
			if (0 == _dwAuthMode)
				_strConnect += CString()
					+ "Persist Security Info=False;"
					+ "Integrated Security=SSPI;";
			else
				_strConnect += CString()
					+ "Persist Security Info=True;"
					+  "User ID='"+ _pszUserName +"';"
					+  "Password='"+ _pszPassword +"';";
		return _strConnect;
	}
// Data
protected:
	CStorage _storage;
	BOOL _isLoaded;
	CString _strConnect;
	char *	_pszSqlServer;
	char *	_pszDatabaseName;
	DWORD	_dwAuthMode;
	char *	_pszUserName;
	char *	_pszPassword;
};

#endif // !defined(AFX_DBOPTIONS_H__E93FD8E0_9334_4962_8768_BDBA90FAA470__INCLUDED_)
